#include"DataStruct.h"
#include "math.h"

#include <ctime>
#include <cstdlib>
#include <iostream>
#include <windows.h>

#define PI 3.1415926
#define UNUSED(x) (void)x;
#define random(x) rand() % (x)
#define SIGN(x) ((x < 0 ) ? 1 : 0 )

int ri1=0;
int dt1=0;
inline double pvp_ai1_angleConver(double angle){
    return -90 - angle;
}


inline double pvp_ai1_vecMul(double x0, double y0, double x1, double y1){
    double vml;
    vml=x0 * x1 + y0 * y1;
    return vml;
}


inline double pvp_ai1_vecMod(double x, double y){
    double vmd;
    vmd=sqrt(x * x + y * y);
    return vmd;
}
//瞄准飞机

//瞄准球
int pvp_ai1_AimBall(DataStruct *data, BallStruct &ball, int &leftward, int &rightward){
    float ball_x = ball.x, ball_y = ball.y, ball_v_x = ball.v_x, ball_v_y = ball.v_y;
    double angleTo, angleDiff,cosPosV;
    if(dt1>3) dt1=0.3;
    else dt1=dt1+0.3;
    ball_x +=ball_v_x*dt1;
    ball_y +=ball_v_y*dt1;
    cosPosV = pvp_ai1_vecMul(ball_v_x, ball_v_y, data->plane1.x - ball_x, data->plane1.y - ball_y)
            / pvp_ai1_vecMod(ball_v_x, ball_v_y) / pvp_ai1_vecMod(data->plane1.x - ball_x, data->plane1.y - ball_y);

    angleTo = pvp_ai1_angleConver(
                (asin(sqrt(1 - cosPosV * cosPosV) * pvp_ai1_vecMod(ball_v_x, ball_v_y) / 2000)
                 + atan2(ball_y - data->plane1.y, ball_x - data->plane1.x)) * 180 / PI);
    angleDiff = fmod(fmod(data->plane1.angle - angleTo, 360) + 360, 360);
    if (angleDiff < 3.6 || angleDiff > 356.4){

        return 1;
    }
    else if (angleDiff < 180){
        leftward = false;
        rightward = true;
    }
    else{
        leftward = true;
        rightward = false;
    }

    return 0;
}


int pvp_ai1_prePlanePos(DataStruct *data, int time, float &x, float &y){
    double v_x0, v_y0, v_x1, v_y1, a_x, a_y;
    v_x0 = data->plane1.v_x;
    v_y0 = data->plane1.v_y;
    if (v_x0 == 0 && v_y0 == 0){
        x = data->plane1.x;
        y = data->plane1.y;
        return 0;
    }
    a_x = -v_x0 / pvp_ai1_vecMod(v_x0, v_y0) * 4000;
    a_y = -v_y0 / pvp_ai1_vecMod(v_x0, v_y0) * 4000;
    v_x1 = v_x0 + a_x * time / 100;
    if (SIGN(v_x1) != SIGN(v_x0)){
        v_x1 = 0;
    }
    v_y1 = v_y0 + a_y * time / 100;
    if (SIGN(v_y1) != SIGN(v_y0)){
        v_y1 = 0;
    }
    x = data->plane1.x + (v_x1 * v_x1 - v_x0 * v_x0) / 2 / a_x;
    y = data->plane1.y + (v_y1 * v_y1 - v_y0 * v_y0) / 2 / a_y;
    return 0;
}


int pvp_ai1_turnTo(DataStruct *data, float x, float y, int &leftward, int &rightward){

    double angleTo, angleDiff;
    angleTo = pvp_ai1_angleConver(atan2(y - data->plane1.y, x - data->plane1.x) * 180 / PI);
    angleDiff = fmod(fmod(data->plane1.angle - angleTo, 360) + 360, 360);
    if (angleDiff < 3.6 || angleDiff > 356.4){
        return 1;
    }
    else if (angleDiff < 180){
        leftward = false;
        rightward = true;
    }
    else{
        leftward = true;
        rightward = false;
    }

    return 0;
}

int pvp_ai1_moveTo(DataStruct *data, float x, float y, int &forward, int &leftward, int &rightward, int precision = 1)
{
    double dis = pvp_ai1_vecMod(y - data->plane1.y, x - data->plane1.x);
    if (dis < precision){
        return 1;
    }

    float pre_x, pre_y;
    pvp_ai1_prePlanePos(data, 3000, pre_x, pre_y);

    if (!pvp_ai1_turnTo(data, data->plane1.x + x - pre_x, data->plane1.y + y - pre_y, leftward, rightward))
    {
        forward = false;
        return 0;
    }

    dis = pvp_ai1_vecMod(y - pre_x, x - pre_y);
    if (dis >= precision)
    {
        forward = true;
        leftward = false;
        rightward = false;
    }

    return 0;
}


int pvp_ai1_moveNeeded(DataStruct *data,KeyStruct*key, int &des_x, int &des_y, int preTime = 200)
{
    float min_t = INT_MAX;
    for (int i = 0; i < data->ball_size; i++)
    {
        BallStruct &ball = data->ball[i];
        float ball_x = ball.x, ball_y = ball.y, ball_v_x = ball.v_x, ball_v_y = ball.v_y;
        float A, B, C;
        A = -1;
        B = ball_v_y / ball_v_x;
        C = ball_y - ball_x * ball_v_y / ball_v_x;
        float dis = fabs((A * data->plane1.y + B * data->plane1.x + C) / pvp_ai1_vecMod(A, B));
        float cross_x = (data->plane1.y - ball_y + ball_v_y / ball_v_x * ball_x + ball_v_x / ball_v_y * data->plane1.x)
                / (ball_v_x / ball_v_y + ball_v_y / ball_v_x);
        float cross_y = (ball_v_y / ball_v_x) * (cross_x - ball_x) + ball_y;
        float t = (cross_x - ball_x) / ball_v_x * 100;
        if (t < 0 || t > preTime)
        {
            continue;
        }

        if(dis < data->plane1.r + ball.r + 2 && t < (min_t-100)){
        key->shield=true;


        }
        if (dis < data->plane1.r + ball.r + 10 && t < min_t )
        {
            min_t = t;
            des_x = data->plane1.x +  ball.r * (data->plane1.x - cross_x)
                    / pvp_ai1_vecMod(data->plane1.x - cross_x, data->plane1.y - cross_y);
            des_y = data->plane1.y +  ball.r * (data->plane1.y - cross_y)
                    / pvp_ai1_vecMod(data->plane1.x - cross_x, data->plane1.y - cross_y);
            if (des_x < data->plane1.r || des_x > 2000 - data->plane1.r)
            {
                des_x = data->plane1.x;
                if (data->plane1.y - cross_y > 0)
                    des_y = cross_y + ball.r + data->plane1.r;
                else
                    des_y = cross_y - ball.r - data->plane1.r;
            }
            if (des_y < data->plane1.r || des_y > 1500 - data->plane1.r)
            {
                des_y = data->plane1.y;
                if (data->plane1.x - cross_x > 0)
                    des_x = cross_x + ball.r + data->plane1.r;
                else
                    des_x = cross_x - ball.r - data->plane1.r;
            }
        }
    }

    if (min_t != INT_MAX)
    {
        return 1;
    }

    return 0;
}

//射击最危险的球
int pvp_ai1_shootNow(DataStruct *data, int preTime = 200){
    if(ri1!=0){
        if(data->ball[ri1].hp>0) return ri1;
    }
    float min_t = INT_MAX;
    int i;
    for (i = 0; i < data->ball_size; i++)
    {
        BallStruct &ball = data->ball[i];
        float ball_x = ball.x, ball_y = ball.y, ball_v_x = ball.v_x, ball_v_y = ball.v_y;
        float A, B, C;
        A = -1;
        B = ball_v_y / ball_v_x;
        C = ball_y - ball_x * ball_v_y / ball_v_x;
        float dis = fabs((A * data->plane1.y + B * data->plane1.x + C) / pvp_ai1_vecMod(A, B));
        float cross_x = (data->plane1.y - ball_y + ball_v_y / ball_v_x * ball_x + ball_v_x / ball_v_y * data->plane1.x)
                / (ball_v_x / ball_v_y + ball_v_y / ball_v_x);

        float t = (cross_x - ball_x) / ball_v_x * 100;
        if (t < 0 || t > preTime)
        {
            if(i==23) return 0;
            continue;

        }
        if (dis < data->plane1.r + ball.r + 10 && t < min_t) {
            ri1=i;
           return ri1;
        }
        else return 0;
    }
    if(i==24) return 0;
}
int shootPlane1(DataStruct*data,int &leftward, int &rightward){
    float plane_x = data->plane2.x, plane_y = data->plane2.y, plane_v_x = data->plane2.v_x, plane_v_y = data->plane2.v_y;
    if(plane_v_x==0) plane_v_x=10;
    if(plane_v_y==0) plane_v_y=10;

    double angleTo, angleDiff,cosPosV;

    cosPosV = pvp_ai1_vecMul(plane_v_x, plane_v_y, data->plane1.x - plane_x, data->plane1.y - plane_y)
            / pvp_ai1_vecMod(plane_v_x, plane_v_y) / pvp_ai1_vecMod(data->plane1.x - plane_x, data->plane1.y - plane_y);

    angleTo = pvp_ai1_angleConver(
                (asin(sqrt(1 - cosPosV * cosPosV) * pvp_ai1_vecMod(plane_v_x, plane_v_y) / 2000)
                 + atan2(plane_y - data->plane1.y, plane_x - data->plane1.x)) * 180 / PI);

    angleDiff = fmod(fmod(data->plane1.angle - angleTo, 360) + 360, 360);

    if (angleDiff < 3.6 || angleDiff > 356.4){

        return 1;
    }
    else if (angleDiff < 180){
        leftward = false;
        rightward = true;
    }
    else{
        leftward = true;
        rightward = false;
    }

    return 0;
}

void ai_pvp_ai1(DataStruct*data,KeyStruct*key){
    key->forward = false;
    key->rotate_left = false;
    key->rotate_right = false;
    key->shoot = true;
    int f2=1;

//瞄准飞机
    if(data->bullet2_size!=0){
        shootPlane1(data,key->rotate_left, key->rotate_right);
    }
//射击type==3的三个大球
    for(int i=0;i<25&&data->bullet2_size==0;i++){
        if(data->ball[i].hp>0&&data->ball[i].type==3) {
            f2=0;
            pvp_ai1_AimBall(data, data->ball[i], key->rotate_left, key->rotate_right);
            break;

        }
    }

    static int moveFlag = 1, des_x, des_y;
//射击最有威胁的球，如果正在射击大球则不进行
    ri1=pvp_ai1_shootNow(data);
    int f1=1;
    if(ri1!=0&&f2==1&&data->bullet2_size==0&&fabs(data->ball[ri1].v_x)<1300&&fabs(data->ball[ri1].v_y)<1300){
        pvp_ai1_AimBall(data, data->ball[ri1], key->rotate_left, key->rotate_right);
        f1=0;
    }
 //躲避最危险的球
    if ((pvp_ai1_moveNeeded(data,key,des_x, des_y) || moveFlag == 0))
    {
        moveFlag = pvp_ai1_moveTo(data, des_x, des_y, key->forward, key->rotate_left, key->rotate_right);
        return;
    }

 //没有大球和危险的球，则射击0号球
     int ff=1;
    if (f1==1&&f2==1&&data->bullet2_size==0&&data->ball_size)
    {

        if(ff==1){
            for(int i=0;i<25;i++){
                if(data->ball[i].hp>0&&data->ball[i].type==2) {

                    pvp_ai1_AimBall(data, data->ball[i], key->rotate_left, key->rotate_right);
                     ff=0;
                    break;
                }
            }
        }
        if(ff==1){
            for(int i=0;i<25;i++){
                if(data->ball[i].hp>0&&data->ball[i].type==1) {

                    pvp_ai1_AimBall(data, data->ball[i], key->rotate_left, key->rotate_right);
                     ff=0;
                    break;
                }
            }
        }
        if(ff==1){

            pvp_ai1_AimBall(data, data->ball[0], key->rotate_left, key->rotate_right);


         }
    }

}

